package com.informationsystem.service.impl;

import com.informationsystem.dao.VideoDao;
import com.informationsystem.model.Video;
import com.informationsystem.service.VideoManager;
import com.informationsystem.service.impl.GenericManagerImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.jws.WebService;

@Service("videoManager")
@WebService(serviceName = "VideoService", endpointInterface = "com.informationsystem.service.VideoManager")
public class VideoManagerImpl extends GenericManagerImpl<Video, Long> implements VideoManager {
    VideoDao videoDao;

    @Autowired
    public VideoManagerImpl(VideoDao videoDao) {
        super(videoDao);
        this.videoDao = videoDao;
    }
    @Override
	public List getVideoList() {
		List<Video> videoList = getAll();
		Collections.sort(videoList, new PostDateDsc());
		return videoList;
	}
	
	private  class PostDateDsc implements  Comparator<Video>{
		@Override
		public int compare(Video o1, Video o2) {
			return o2.getPostDate().compareTo(o1.getPostDate());
		}
	}

	@Override
	public List getTop2VideoList() {
		List videoList = getVideoList();
		if(videoList.size()>2)
		videoList = videoList.subList(0, 2);
		return videoList;
	}
}